<?php


namespace app\admin\model\base;


use app\BaseModel;

class Paper extends BaseModel
{
    public $append = ['img_url'];
    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @param string $field
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = '', string $field = '*'): array
    {
        //查询
        $data = self::field($field)
            ->with(['special','paper_subjects'])
            ->where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        return $data;
    }

    /**
     * 获取列表2
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @param string $field
     * @return array
     */
    public static function getList2(array $where, int $page, int $limit, string $orderby = '', string $field = '*'): array
    {
        //查询
        $data = self::field($field)
            ->with(['special'])
            ->where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        return $data;
    }

    public function getImgUrlAttr($value){
        return env('ApiImagesUrl').$this->img;
    }

    /**
     * 关联分类
     * @return \think\model\relation\HasOne
     */
    public function paperCate(){
        return $this->hasOne(PaperCate::class,'id','paper_cate_id');
    }

    /**
     * 关联题目
     * @return \think\model\relation\HasOne
     */
    public function paperSubjects(){
        return $this->hasMany(PaperSubject::class,'paper_id','id')->where(['is_del'=>0]);
    }

    /**
     * 关联专业
     * @return \think\model\relation\HasOne
     */
    public function special(){
        return $this->hasOne(Special::class,'id','special_id')->field('id,name');
    }
}